//package com.yy.cloud.loadbalancer;
//
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.ObjectProvider;
//import org.springframework.cloud.client.ServiceInstance;
//import org.springframework.cloud.client.loadbalancer.DefaultResponse;
//import org.springframework.cloud.client.loadbalancer.EmptyResponse;
//import org.springframework.cloud.client.loadbalancer.reactive.Request;
//import org.springframework.cloud.client.loadbalancer.reactive.Response;
//import org.springframework.cloud.loadbalancer.core.ReactorServiceInstanceLoadBalancer;
//import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
//import reactor.core.publisher.Mono;
//
//import java.util.List;
//import java.util.concurrent.atomic.AtomicInteger;
//
///**
// * 自定义负载均衡算法
// *
// * @date 2024/5/6
// */
//@Slf4j
//public class CustomLoadBalancer implements ReactorServiceInstanceLoadBalancer {
//
//    private ObjectProvider<ServiceInstanceListSupplier> serviceInstanceListSuppliers;
//    private String serviceName;
//
//    public CustomLoadBalancer(ObjectProvider<ServiceInstanceListSupplier> serviceInstanceListSuppliers, String serviceName) {
//        this.serviceName = serviceName;
//        this.serviceInstanceListSuppliers = serviceInstanceListSuppliers;
//    }
//
//    private AtomicInteger atomicCount = new AtomicInteger(0);
//    private AtomicInteger atomicCurrentCount = new AtomicInteger(0);
//
//    public Mono<Response<ServiceInstance>> choose(Request request) {
//        ServiceInstanceListSupplier supplier = serviceInstanceListSuppliers.getIfAvailable();
//        return supplier.get().next().map(this::getInstanceResponse);
//
//    }
//
//    /**
//     * 自定义算法获取服务实例
//     *
//     * @param instances 所有可用实例列表
//     * @return 服务实例
//     */
//    private Response<ServiceInstance> getInstanceResponse(List<ServiceInstance> instances) {
//        ServiceInstance serviceInstance = null;
//        if (instances.isEmpty()) {
//            log.warn("注册中心无可用实例：" + serviceName);
//            org.springframework.cloud.client.loadbalancer.Response response = new EmptyResponse();
//            return (Response<ServiceInstance>) response;
//        }
//        // 累加并得到值(请求次数)
//        int requestCount = atomicCount.incrementAndGet();
//
//        if (requestCount < 2) {
//            serviceInstance = instances.get(atomicCurrentCount.get());
//        } else {
//            // 已经大于2了，重置
//            atomicCount = new AtomicInteger(0);
//            // atomicCurrentCount变量+1
//            atomicCurrentCount.incrementAndGet();
//
//            if (atomicCurrentCount.get() >= instances.size()) {
//                atomicCurrentCount = new AtomicInteger(0);
//                serviceInstance = instances.get(instances.size() - 1);
//                return (Response<ServiceInstance>) new DefaultResponse(serviceInstance);
//            }
//
//            // 从可用的实例中获取一个实例来进行操作，类似轮询算法
//            serviceInstance = instances.get(atomicCurrentCount.get() - 1);
//        }
//        return (Response<ServiceInstance>) new DefaultResponse(serviceInstance);
//    }
//
//}
